import axios from 'axios'
import {MessageBox,Message} from 'element-ui'
import cookieApi from 'js-cookie'
// 创建axios实例
const service = axios.create({
  baseURL: 'http://localhost:9001', // api的base_url
  timeout: 20000 // 请求超时时间
})

//http request 拦截器
service.interceptors.request.use(
  config => {
    if(cookieApi.get('usertoken')){
      config.headers['usertoken'] = cookieApi.get('usertoken');
    }
      return config
  },
  err => {
    return Promise.reject(err);
  }
)

//http response 拦截器
service.interceptors.response.use(
  response => {
    if(response.data.code == 28004){
      console.log("response.data.resultCode是28004")
      window.location.href="/login"
      return
    }else{
      if(response.data.code !== 20000){
        if(response.data.code != 25000){
          Message({
            message:response.data.message || 'error',
            type: 'error',
            duration: 5*1000
          })
        }
      }else{
        return response;
      }
    }
  },
  error => {
    return Promise.reject(error.response) //返回接口返回的错误信息
  }
);

export default service